package com.microtf.inscription.web.controller;

import com.microtf.inscription.framework.exception.BusinessException;
import com.microtf.inscription.framework.service.WeChatService;
import com.microtf.inscription.web.service.WeChatListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 微信相关接口
 *
 * @author guliuzhong
 * @author guliuzhong
 * @since 1.0
 */
@Controller
@RequestMapping("/wxapi")
@Slf4j
public class WxApiController extends BaseController{

//    Manager manager;
//    WxUploadService wxUploadService;
//    BaiduAiApiService baiduAiApiService;
    WeChatService weChatService;


    WeChatListener weChatListener;

//    @Autowired
//    @Qualifier(value = "wxUpload")
//    public void setManager(Manager manager) {
//        this.manager = manager;
//    }
//
//    @Autowired
//    public void setBaiduAiApiService(BaiduAiApiService baiduAiApiService) {
//        this.baiduAiApiService = baiduAiApiService;
//    }
//
//    @Autowired
//    public void setWxUploadService(WxUploadService wxUploadService) {
//        this.wxUploadService = wxUploadService;
//    }

    @Autowired
    public void setWeChatService(WeChatService weChatService) {
        this.weChatService = weChatService;
    }

    @Autowired
    public void setWeChatListener(WeChatListener weChatListener) {
        this.weChatListener = weChatListener;
    }

//    @PostMapping(value = "/{appID}/upload", produces = "application/json;charset=utf-8")
//    @ResponseBody
//    public WxResponse upload(@PathVariable("appID") String appId, @RequestParam("file") MultipartFile file) {
//        String originalFilename = file.getOriginalFilename();
//        assert originalFilename != null;
//        originalFilename = originalFilename.replaceAll(":", "").replaceAll("\\//", "").replaceAll("\\\\", "");
//        WxResponse.WxResponseBuilder builder = WxResponse.builder();
//        try {
//            byte[] bytes = file.getBytes();
//            String s = DigestUtils.md5DigestAsHex(bytes);
//            StorageObject storageObject = wxUploadService.uploadFile(bytes, s);
//            WxUpload upload = wxUploadService.upload(appId, storageObject.getUrl(), storageObject.getObjectName());
//            builder.data(String.valueOf(upload.getId())).isSuccess(true);
//        } catch (IOException | ManagerException e) {
//            log.error("图片上传出错原因"+e.getMessage());
//            builder.isSuccess(false).message("图片上传出错原因：服务器出错");
//        }
//        return builder.build();
//    }
//    @RequestMapping(value = "/{appID}/getPlant", produces = "application/json;charset=utf-8")
//    @ResponseBody
//    @Transactional(rollbackFor = {})
//    public WxResponse getPlant( @PathVariable("appID") String appId,@RequestParam("id") Long id, @RequestParam("baike_num") Integer baikeNum) {
//        WxResponse.WxResponseBuilder builder = WxResponse.builder();
//        Optional<WxUpload> byId = wxUploadService.findById(id);
//        if (!byId.isPresent()) {
//            return builder.message("未找到上传的文件").isSuccess(false).build();
//        } else {
//            WxUpload wxUpload = byId.get();
//            if(wxUpload.getProcessText()!=null){
//                return builder.data(wxUpload.getProcessText()).isSuccess(true).build();
//            }
//            String bodySeg = null;
//            try {
//                bodySeg = baiduAiApiService.plant(URI.create(wxUpload.getUrl()),baikeNum);
//                wxUploadService.process(wxUpload,"plant","true",bodySeg);
//                return builder.data(bodySeg).isSuccess(true).build();
//            } catch (IOException e) {
//                return builder.message("图片识别出错").isSuccess(false).build();
//            }
//        }
//    }
    @RequestMapping(value = "/{appId}/interface",produces = {"application/xml;charset=utf-8"})
    @ResponseBody
    @Transactional(rollbackFor = {})
    public String wxInterface(@PathVariable(name = "appId") Integer appId, HttpServletRequest httpServletRequest) {
        weChatService.init(appId, weChatListener);
        try {
            String s = weChatService.handleMessage(httpServletRequest);
            log.info(s);
            return s;
        }catch (BusinessException exception){
            log.error(exception.getMessage());
            throw exception;
        }finally {
            weChatService.release();
        }
    }
}
